# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.core.paginator import Paginator
from django.core.paginator import PageNotAnInteger
from django.core.paginator import EmptyPage
from django.template import  RequestContext
from django import forms
import presson.teacher
import presson.student
import classes.classinfo
import lesson.mangelesson
import zidian.zidian
import System.sign
import System.rollcall
import System.upload
import System.election
import System.count
import System.upload
import news.news
import datetime
import django.http
import Cust.global_dictionary
from django.http import response
from django.http import StreamingHttpResponse
import StringIO
import os
import json
from django.http import HttpResponse
from xlwt import *
import xlrd

# Create your views here.

def showIndex(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
         user_name = request.session.get('user_name',default=None)
         countinfo = System.count.Count()
         count = countinfo.getcount()
         print count
         return render_to_response('index.html',{'user_name': user_name,'count':count})
def checkuser(request):
     username = request.GET.get('username')
     password = request.GET.get('password')
     systemiofo = System.sign.User()
     sys = systemiofo.queryUser(username,password)
     if sys==1:
        request.session['user_name'] = username
        request.session['user_type'] = sys
        countinfo = System.count.Count()
        count = countinfo.getcount()
        print count
        return render(request,'index.html',{'user_name':username,'count':count})
     elif sys==0:
        request.session['user_name'] = username
        request.session['user_type'] = sys
        countinfo = System.count.Count()
        count = countinfo.getcount()
        print count
        return render(request,'index.html',{'user_name':username,'count':count})
     else:
        return render(request,'sign-in.html',context={'result':1})
# lesson action
def showlesson(request):
     if request.session.get('user_name',default=None) == None:
        return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        lessoninfo = lesson.mangelesson.LessonMange()
        lessons  = lessoninfo.getAllLesson()
        result = []
        for les in lessons:
              result.append({'id':les['Lesson_Id'],'No':les['Lesson_Name'],'Teacher':les['Teacher'],'Term':les['Term'],'Week':les['Week'],'Weeks':les['WeekDay'],'StartSection':les['StartSection'],'EndSection':les['EndSection'],'ClassRoom':les['ClassRoom'],'grade':les['grade'],'academy':les['academy']})
        paginator = Paginator(result, 5)
        page = request.GET.get('page')
        if page==None:
           page=1
        try:
           result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'lesson.html',context={'result':result,'user_name': user_name})
def lessonadd(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
         user_name = request.session.get('user_name',default=None)
         teacherinfo = presson.teacher.Teacher()
         teacher  = teacherinfo.getAllTeacher()
         result = []
         for tea in teacher:
            result.append({'id':tea['id'],'name':tea['name']})
         return render(request,'lesson_add.html',context={'result':result,'user_name': user_name})
def showsign_in(request):

     return render_to_response('sign-in.html')
def showreset_password(request):
     return render_to_response('reset-password.html')
def getlesson(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        classname = request.GET.get('classname')
        Teachername = request.GET.get('Teachername')
        Term = request.GET.get('Term')
        weeks = request.GET.get('weeks')
        startcourse = request.GET.get('startcourse')
        endcourse = request.GET.get('endcourse')
        week = request.GET.get('week')
        adress = request.GET.get('adress')
        grade = request.GET.get('grade')
        academy = request.GET.get('academy')
        lessoninfo = lesson.mangelesson.LessonMange()
        lessons =lessoninfo.insertLesson(classname,Teachername,Term,weeks,week,startcourse,endcourse,adress,academy,grade)
        if lessons==111:
            return render(request,'premium-build.html',context={'result':111,'user_name': user_name})
        else:
            return render(request,'premium-build.html',context={'result':110,'user_name': user_name})
def updatelesson(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        classid = request.GET.get('id')
        lessoninfo = lesson.mangelesson.LessonMange()
        lessons =lessoninfo.queryLesson(classid)
        teacherinfo = presson.teacher.Teacher()
        teacher  = teacherinfo.getAllTeacher()
        result = []
        for tea in teacher:
            result.append({'id':tea['id'],'name':tea['name']})
        return render(request,'lesson_update.html',context={'result':result,'turn':lessons,'user_name': user_name})
def lessonupdate(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        classid = request.GET.get('id')
        classname = request.GET.get('classname')
        Teachername = request.GET.get('Teachername')
        Term = request.GET.get('Term')
        weeks = request.GET.get('weeks')
        startcourse = request.GET.get('startcourse')
        endcourse = request.GET.get('endcourse')
        week = request.GET.get('week')
        adress = request.GET.get('adress')
        grade = request.GET.get('grade')
        academy = request.GET.get('academy')
        lessoninfo = lesson.mangelesson.LessonMange()
        lessons =lessoninfo.modifyLesson(classid,classname,Teachername,Term,weeks,week,startcourse,endcourse,adress,academy,grade)
        if lessons==130:
            return render(request,'premium-build.html',context={'result':130,'user_name': user_name})
        else:
            return render(request,'premium-build.html',context={'result':131,'user_name': user_name})
def dellesson(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        classid = request.GET.get('id')
        lessoninfo = lesson.mangelesson.LessonMange()
        lessons  = lessoninfo.deLesson(classid)
        if lessons==120:
            return render(request,'premium-build.html',context={'result':120,'user_name': user_name})
        else:
            return render(request,'premium-build.html',context={'result':121,'user_name': user_name})
# teacher action
def teacheradd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
         user_name = request.session.get('user_name',default=None)
         return render(request,'teacher_add.html',{'user_name': user_name})
def showteacher(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        teacherinfo = presson.teacher.Teacher()
        teacher  = teacherinfo.getAllTeacher()
        paginator = Paginator(teacher, 10)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'teacher.html',context={'result':result,'user_name': user_name})
def getteacher(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        teachername = request.GET.get('teachername')
        teacherno = request.GET.get('teacherno')
        teacherinfo = presson.teacher.Teacher()
        teacher =teacherinfo.inserteacher(teachername,teacherno)
        if teacher==210:
            return render(request,'premium-blog.html',context={'result':210,'user_name': user_name})
        else:
            return render(request,'premium-blog.html',context={'result':211,'user_name': user_name})
def updateteacher(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        teacherinfo = presson.teacher.Teacher()
        teacher =teacherinfo.getTeacher(id)
        return render(request,'teacher_update.html',context={'result':teacher,'user_name': user_name})
def teacherupdate(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        print id
        Teachername = request.GET.get('teachername')
        TeacherNo = request.GET.get('teacherno')
        teacherinfo = presson.teacher.Teacher()
        teacher =teacherinfo.modifyteacher(id,Teachername,TeacherNo)
        if teacher==230:
            return render(request,'premium-blog.html',context={'result':230,'user_name': user_name})
        else:
            return render(request,'premium-blog.html',context={'result':231,'user_name': user_name})
def delteacher(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        teacherinfo = presson.teacher.Teacher()
        teacher =teacherinfo.delteacher(id)
        if teacher==220:
            return render(request,'premium-blog.html',context={'result':220,'user_name': user_name})
        else:
            return render(request,'premium-blog.html',context={'result':221,'user_name': user_name})
# student action
def studentadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'student_add.html',context={'result':result,'user_name': user_name})
def showstudent(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        studentinfo = presson.student.Student()
        student  = studentinfo.getAllStudent()
        paginator = Paginator(student, 40)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'student.html',context={'result':result,'user_name': user_name})
def getstudent(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        studentname = request.GET.get('studentname')
        classname = request.GET.get('classname')
        studentno = request.GET.get('studentno')
        studentinfo = presson.student.Student()
        student =studentinfo.insertStu(studentname,studentno,classname)
        if student==310:
            return render(request,'premium-blog-item.html',context={'result':310,'user_name': user_name})
        else:
            return render(request,'premium-blog-item.html',context={'result':311,'user_name': user_name})
def updatestudent(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        studentinfo = presson.student.Student()
        student =studentinfo.getStudent(id)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        banji = []
        for tea in classs:
            banji.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'student_update.html',context={'result':banji,'student':student,'user_name': user_name})
def studentupdate(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        studentname = request.GET.get('studentname')
        studentno = request.GET.get('studentno')
        classname = request.GET.get('classname')
        studentinfo = presson.student.Student()
        student =studentinfo.modifystudent(id,studentname,studentno,classname)
        if student==330:
            return render(request,'premium-blog-item.html',context={'result':330,'user_name': user_name})
        else:
            return render(request,'premium-blog-item.html',context={'result':331,'user_name': user_name})
def delstudent(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        studentinfo = presson.student.Student()
        student =studentinfo.delstudent(id)
        if student==320:
            return render(request,'premium-blog-item.html',context={'result':320,'user_name': user_name})
        else:
            return render(request,'premium-blog-item.html',context={'result':321,'user_name': user_name})
# class action
def classadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'class_add.html',context={'result':result,'user_name': user_name})
def showclass(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinfo = classes.classinfo.ClassInfo()
        classs  = classinfo.getAllClass()
        paginator = Paginator(classs, 10)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'class.html',context={'result':result,'user_name': user_name})
def getclass(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        classname = request.GET.get('classname')
        collegename = request.GET.get('collegename')
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.insertClass(classname,collegename)
        if classs==610:
            return render(request,'premium-colors.html',context={'result':610,'user_name': user_name})
        else:
            return render(request,'premium-colors.html',context={'result':611,'user_name': user_name})
def updateclass(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        print id
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getClass(id)
        print classs
        return render(request,'class_update.html',context={'result':classs,'user_name': user_name})
def classupdate(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        classname = request.GET.get('classname')
        classacademy = request.GET.get('academy')
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.modifyClass(id,classname,classacademy)
        if classs==630:
            return render(request,'premium-colors.html',context={'result':630,'user_name': user_name})
        else:
            return render(request,'premium-colors.html',context={'result':631,'user_name': user_name})
def delclass(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.delClass(id)
        if classs==620:
            return render(request,'premium-colors.html',context={'result':620,'user_name': user_name})
        else:
            return render(request,'premium-colors.html',context={'result':621,'user_name': user_name})
# dictionary action
def dictadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'dict_add.html',context={'result':result,'user_name': user_name})
def showdict(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        dictinfo = zidian.zidian.Zidian()
        dict =dictinfo.getAll()
        paginator = Paginator(dict, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'dict.html',context={'result':result,'user_name': user_name})
def getdict(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        dictname = request.GET.get('dictname')
        dictvalue = request.GET.get('dictvalue')
        dicttype = request.GET.get('dicttype')
        dictinfo = zidian.zidian.Zidian()
        dict =dictinfo.insertzidan(dicttype,dictname,dictvalue)
       
        if dict==710:
            return render(request,'premium-invoice.html',context={'result':710,'user_name': user_name})
        else:
            return render(request,'premium-invoice.html',context={'result':711,'user_name': user_name})
def updatedict(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        dictinfo = zidian.zidian.Zidian()
        dict =dictinfo.getzidian(id)
        return render(request,'dict_update.html',context={'result':dict,'user_name': user_name})
def dictupdate(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        dictname = request.GET.get('dictname')
        dictvalue = request.GET.get('dictvalue')
        dicttype = request.GET.get('dicttype')
        dictinfo = zidian.zidian.Zidian()
        dict =dictinfo.modifyzidian(id,dicttype,dictname,dictvalue)
        if dict==730:
            return render(request,'premium-invoice.html',context={'result':730,'user_name': user_name})
        else:
            return render(request,'premium-invoice.html',context={'result':731,'user_name': user_name})
def deldict(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        dictinfo = zidian.zidian.Zidian()
        dict =dictinfo.delzidain(id)
        if dict==720:
            return render(request,'premium-invoice.html',context={'result':720,'user_name': user_name})
        else:
            return render(request,'premium-invoice.html',context={'result':721,'user_name': user_name})
# user action
def useradd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'user_add.html',context={'result':result,'user_name': user_name})
def showuser(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        userinfo = System.sign.User()
        user = userinfo.getAllUser()
        paginator = Paginator(user, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'user.html',context={'result':result,'user_name': user_name})
def getuser(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        username = request.GET.get('username')
        userinfo = System.sign.User()
        user = userinfo.insertAdmin(username)
        print user
        if user==810:
            return render(request,'premium-media.html',context={'result':810,'user_name': user_name})
        else:
            return render(request,'premium-media.html',context={'result':811,'user_name': user_name})
def updateuser(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        userinfo = System.sign.User()
        user = userinfo.getUser(id)
        return render(request,'user_update.html',context={'result':user,'user_name': user_name})
def userupdate(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        username = request.GET.get('username')
        password = request.GET.get('password')
        userinfo = System.sign.User()
        if password=='1':
            userinfo.resetPassword(id)
        user =userinfo.moifyName(id,username)
        if user==830:
            return render(request,'premium-media.html',context={'result':830,'user_name': user_name})
        else:
            return render(request,'premium-media.html',context={'result':831,'user_name': user_name})
def deluser(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        userinfo = System.sign.User()
        user = userinfo.delAdmin(id)
        if user==820:
            return render(request,'premium-media.html',context={'result':820,'user_name': user_name})
        else:
            return render(request,'premium-media.html',context={'result':821,'user_name': user_name})
# password action
def updatepassword(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)

        return render(request,'password_update.html',context={'user_name': user_name})
def passwordupdate(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        ops = request.GET.get('opassword')
        nps1 = request.GET.get('npassword1')
        nps2 = request.GET.get('npassword2')
        print nps1,nps2
        userinfo = System.sign.User()
        ops = userinfo.md5(ops)
        user = userinfo.getUserByname(user_name)
        if ops==user['password']:
            if nps1==nps2:
                u = userinfo.modifyPassword(user['id'],nps1)
                if u==850:
                     return render(request,'sign-in.html',context={'result':2})
                else:
                     return render(request,'password_update.html',context={'result':3})
            else:
                return render(request,'password_update.html',context={'result':1})
        else:
            return render(request,'password_update.html',context={'result':2})
def signout(request):
    if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
    else:
        request.session['user_name'] = None
        return  render(request,'sign-in.html')
# rollcall action
def showteacherrollcall(request):
    if request.session.get('user_name',default=None) == None:
        return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        teacherinfo = presson.teacher.Teacher()
        teacher  = teacherinfo.getAllTeacher()
        paginator = Paginator(teacher, 10)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'teacher_rollcall.html',context={'result':result,'user_name': user_name})

def showlessonrollcall(request):
    if request.session.get('user_name',default=None) == None:
        return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        lessoninfo = System.rollcall.RollCall()
        lesson  = lessoninfo.getAllLesson(id)
        paginator = Paginator(lesson, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'lesson_rollcall.html',context={'result':result,'user_name': user_name,'teacherid':id})
def showrollcall(request):
    if request.session.get('user_name',default=None) == None:
        return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        rollcallinfo = System.rollcall.RollCall()
        rollcall  = rollcallinfo.getRollCall(id)
        print rollcall
        paginator = Paginator(rollcall, 10)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'rollcall.html',context={'result':result,'user_name': user_name,'lessonid':id})
def rollcallinfo(request):
    if request.session.get('user_name',default=None) == None:
        return  render(request,'sign-in.html')
    else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        rollcallinfo = System.rollcall.RollCall()
        rollcall  = rollcallinfo.getRollCallInfo(id)
        print rollcall

        return render(request,'rollcallinfo.html',context={'result':rollcall,'user_name': user_name,'rollcallid':id})
# file action
def uploadpic(request):
     file = request.FILES['file']
     upload= System.upload.UploadFile()
     result = upload.upload_file(file)
     if result==0:
         result = 'Upload Success'


     return response.HttpResponse(result)
def downloadpic(request):
    name = request.GET.get('name')
    down = System.upload.UploadFile()
    res = down.downLoadpic(name)
    return response.HttpResponse(res,content_type='image/jpeg')
# news action
def newsadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'news_add.html',context={'result':result,'user_name': user_name})

def addnews(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        firsttitle = request.POST['firsttitle']
        file = request.FILES['pic']
        up = System.upload.UploadFile()
        upimg = up.upload_file(file)
        img = json.loads(upimg)[0]['picture']
        secondtitle = request.POST['secondtitle']
        type = request.POST['type']
        content = request.POST['content']
        top = request.POST['top']
        if(top=='1'):
            top=True
        else:
            top=False
        type = int(type)
        down = System.upload.UploadFile()
        res = down.makeHtml(content,img) 
        com = 'http://www.carpenterdormon.org/'
        resu = com+res
        newinfo = news.news.News()
        new = newinfo.insertNews(firsttitle,secondtitle,img,type,resu,top)
        if new==510:
            return render(request,'premium-pricing-tables.html',context={'result':510,'user_name': user_name})
        else:
            return render(request,'premium-pricing-tables.html',context={'result':511,'user_name': user_name})
def shownews(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        newinfo = news.news.News()
        new = newinfo.getNews()
        print new
        paginator = Paginator(new, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'news.html',context={'result':result,'user_name': user_name})
def delnews(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        newinfo = news.news.News()
        new = newinfo.delNews(id)
        if new==520:
            return render(request,'premium-pricing-tables.html',context={'result':520,'user_name': user_name})
        else:
            return render(request,'premium-pricing-tables.html',context={'result':521,'user_name': user_name})
# mmd action
def mmdadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        newinfo = news.news.News()
        new = newinfo.getNews()
        con = newinfo.queryCon()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'mmd_add.html',context={'result':result,'news':new,'con':con,'user_name': user_name})
def addmmd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        firsttitle = request.POST['firsttitle']
        secondtitle = request.POST['secondtitle']
        newsname = request.POST['newname']
        classname = request.POST['classname']
        starttime = request.POST['starttime']
        
        file = request.FILES['pic']
        up = System.upload.UploadFile()
        upimg = up.upload_file(file)
        img = json.loads(upimg)[0]['picture']
        chat = request.POST['chat']
        print starttime
        date_time = datetime.datetime.strptime(starttime,'%Y-%m-%d %H:%M')
        newinfo = news.news.News()
        new = newinfo.insertMMD(firsttitle,secondtitle,img,newsname,classname,date_time,chat)
        print new
        if new==910:
            return render(request,'premium-profile.html',context={'result':910,'user_name': user_name})
        else:
            return render(request,'premium-profile.html',context={'result':911,'user_name': user_name})
def showmmd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        newinfo = news.news.News()
        new = newinfo.querymmd()
        print new
        paginator = Paginator(new, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'mmd.html',context={'result':result,'user_name': user_name})
def delmmd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        newinfo = news.news.News()
        new = newinfo.delmmd(id)
        if new==920:
            return render(request,'premium-profile.html',context={'result':920,'user_name': user_name})
        else:
            return render(request,'premium-profile.html',context={'result':921,'user_name': user_name})
# acade action
def acadeadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        newinfo = news.news.News()
        new = newinfo.getNews()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'acade_add.html',context={'result':result,'news':new,'user_name': user_name})
def addacade(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        collegename = request.GET.get('collegename')
        newsname = request.GET.get('newname')
        newinfo = news.news.News()
        new = newinfo.insertActity(collegename,newsname)
        if new==1010:
            return render(request,'premium-search-results.html',context={'result':1010,'user_name': user_name})
        else:
            return render(request,'premium-search-results.html',context={'result':1011,'user_name': user_name})
def showacade(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        newinfo = news.news.News()
        new = newinfo.queryactivity()
        print new
        paginator = Paginator(new, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'acade.html',context={'result':result,'user_name': user_name})
def delacade(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        print id
        newinfo = news.news.News()
        new = newinfo.delActivity(id)
        if new==1020:
            return render(request,'premium-search-results.html',context={'result':1020,'user_name': user_name})
        else:
            return render(request,'premium-search-results.html',context={'result':1021,'user_name': user_name})
# election action
def electionadd(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        newinfo = news.news.News()
        new = newinfo.getNews()
        con = newinfo.queryCon()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'election_add.html',context={'result':result,'news':new,'con':con,'user_name': user_name})
def addelection(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        eletitle = request.GET.get('eletitle')
        eledetail = request.GET.get('eledetail')
        classname = request.GET.get('classname')
        elelim = request.GET.get('elelim')
        eletype = request.GET.get('eletype')
        eleano = request.GET.get('eleano')
        starttime = request.GET.get('starttime')
        endtime = request.GET.get('endtime')
        elelim = int(elelim)
        eletype = int(eletype)
        if eleano=='True':
            eleano=True
        if eleano=='False':
            eleano=False
        sdate_time = datetime.datetime.strptime(starttime,'%Y-%m-%d %H:%M')
        edate_time = datetime.datetime.strptime(endtime,'%Y-%m-%d %H:%M')
        print sdate_time,edate_time
        eleinfo = System.election.Election()
        election = eleinfo.insertElection(eletitle,classname,sdate_time,edate_time,eletype,eleano,elelim,eledetail)
        print election
        if election==1110:
            return render(request,'premium-upgrade-account.html',context={'result':1110,'user_name': user_name})
        else:
            return render(request,'premium-upgrade-account.html',context={'result':1111,'user_name': user_name})
def showelection(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        eleinfo = System.election.Election()
        election = eleinfo.queryElection()
        paginator = Paginator(election, 5)
        page = request.GET.get('page')
        if page==None:
            page=1
        try:
            result = paginator.page(page)
        except PageNotAnInteger:
            result = paginator.page(1)
        except EmptyPage:
            result = paginator.page(paginator.num_pages)
        return render(request,'election.html',context={'result':result,'user_name': user_name})
def delelection(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        eleinfo = System.election.Election()
        election = eleinfo.delelection(id)
        if election==1220:
            return render(request,'premium-upgrade-account.html',context={'result':1220,'user_name': user_name})
        else:
            return render(request,'premium-upgrade-account.html',context={'result':1221,'user_name': user_name})
def goelection(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        id = request.GET.get('id')
        classid = request.GET.get('classid')
        eleinfo = System.election.Election()
        election = eleinfo.querystu(classid)
        print election
        return render(request,'electioninfo.html',context={'result':election,'eleid':id,'user_name': user_name})
def goelestu(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        stu = request.GET.get('stu')
        eleid = request.GET.get('id')
        check_box_list = request.GET.getlist("che")
        print eleid,check_box_list
        eleinfo = System.election.Election()
        election = eleinfo.insertElectionInfo(eleid,check_box_list)
        if election==1210:
            return render(request,'premium-upgrade-account.html',context={'result':1210,'user_name': user_name})
        else:
            return render(request,'premium-upgrade-account.html',context={'result':1211,'user_name': user_name})
 # excel action
def excel_export(request):
     id = request.GET.get('id')
     rollcallinfo = System.rollcall.RollCall()
     rollcall  = rollcallinfo.getRollCallInfo(id)
     if rollcall:
        ws = Workbook(encoding='utf-8')
        w = ws.add_sheet(u"数据报表第一页")
        w.write(0, 0, u"学生学号")
        w.write(0, 1, u"学生姓名")
        w.write(0, 2, u"出勤情况")

        excel_row = 1
        for obj in rollcall:
            data_no = obj['no']
            data_name = obj['name']
            data_status = obj['status']
            w.write(excel_row, 0, data_no)
            w.write(excel_row, 1, data_name)
            if data_status==0:
                w.write(excel_row, 2, '已出勤')
            else:
                w.write(excel_row, 2, '未出勤')
            excel_row += 1

        exist_file = os.path.exists("data.xls")
        if exist_file:
            os.remove(r"data.xls")
        ws.save("data.xls")
        ############################
        sio = StringIO.StringIO()
        ws.save(sio)
        sio.seek(0)
        response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-excel')
        response['Content-Disposition'] = 'attachment; filename=data.xls'
        response.write(sio.getvalue())
        return response
def expert_excle(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        user_name = request.session.get('user_name',default=None)
        classinf = classes.classinfo.ClassInfo()
        classs  = classinf.getAllClass()
        result = []
        for tea in classs:
            result.append({'id':tea['id'],'name':tea['classname']})
        return render(request,'excel_upload.html',context={'result':result,'user_name': user_name})

def uploadstudents(request):
     classname = request.POST['classname']
     f= request.FILES['excel']
     book = xlrd.open_workbook(file_contents=f.read())
     sheet =book.sheet_by_index(0)
     nrows = sheet.nrows
     studentinfo = presson.student.Student()
     for i in range(nrows):
         cell_value1 =int(sheet.cell_value(i,0))
         cell_value1 =str( cell_value1)
         cell_value2 =sheet.cell_value(i,1)
         student =studentinfo.insertStu(cell_value2,cell_value1,classname)
         if student==311:
             break
     user_name = request.session.get('user_name',default=None)
     classinf = classes.classinfo.ClassInfo()
     classs  = classinf.getAllClass()
     result = []
     for tea in classs:
          result.append({'id':tea['id'],'name':tea['classname']})
     return render(request,'excel_upload.html',context={'result':result,'user_name': user_name,'warn':student})
def uploadteacher(request):
     file = request.FILES['excel']
     book = xlrd.open_workbook(file_contents=file.read())
     sheet =book.sheet_by_index(0)
     nrows = sheet.nrows
     teacherinfo = presson.teacher.Teacher()
     for i in range(nrows):
         cell_value1 =int(sheet.cell_value(i,0))
         cell_value1 =str( cell_value1)
         cell_value2 =sheet.cell_value(i,1)
         teacher =teacherinfo.inserteacher(cell_value2,cell_value1)
         if teacher==211:
             break
     user_name = request.session.get('user_name',default=None)
     classinf = classes.classinfo.ClassInfo()
     classs  = classinf.getAllClass()
     result = []
     for tea in classs:
          result.append({'id':tea['id'],'name':tea['classname']})
     return render(request,'excel_upload.html',context={'result':result,'user_name': user_name,'warn':teacher})
def uploadclass(request):
     collegename = request.POST['collegename']
     file = request.FILES['excel']
     book = xlrd.open_workbook(file_contents=file.read())
     sheet =book.sheet_by_index(0)
     nrows = sheet.nrows
     classinf = classes.classinfo.ClassInfo()
     print nrows
     for i in range(nrows):
         cell_value1 =int(sheet.cell_value(i,0))
         cell_value1 = str(cell_value1)
         print cell_value1,collegename
         cla  = classinf.insertClass(cell_value1,collegename)
         if cla==611:
             break
     user_name = request.session.get('user_name',default=None)
     classs  = classinf.getAllClass()
     result = []
     for tea in classs:
          result.append({'id':tea['id'],'name':tea['classname']})
     return render(request,'excel_upload.html',context={'result':result,'user_name': user_name,'warn':cla})
# 新闻功能添加
def shownew(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        return render(request,'election.html')
def pushnew(request):
     if request.session.get('user_name',default=None) == None:
         return  render(request,'sign-in.html')
     else:
        return render(request,'election.html')